package com.xuecheng.media.controller;

import com.xuecheng.commons.model.dto.*;
import com.xuecheng.commons.model.vo.ResponseResult;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 媒资信息前端控制器
 * </p>
 *
 * @author HuangYuwu
 */
@Slf4j
@RestController
public class MediaController {

    @Autowired
    private MediaService  mediaService;

    /**
     * 获取阿里云上传token
     * @param vodUploadDto
     * @return
     */
    @PostMapping("/media/vod-token")
    public ResponseResult vodTokenMedia(@RequestBody VodUploadDto vodUploadDto) throws Exception {
        return mediaService.vodTokenMedia(vodUploadDto);
    }

    /**
     * 刷新阿里云上传token
     * @param videoId
     * @return
     */
    @GetMapping("/media/refresh-vod-token/{videoId}")
    public ResponseResult refreshvodTokenMedia(@PathVariable String videoId) throws Exception {
        return mediaService.refreshvodTokenMedia(videoId);
    }

    /**
     * 保存媒资信息
     * @param mediaDto
     * @return
     */
    @PostMapping("/media")
    public ResponseResult saveMedia(@RequestBody MediaDto mediaDto) throws Exception {
        return mediaService.saveMedia(mediaDto);
    }

    /**
     *分页查询媒资信息
     * @param pageRequest
     * @param mediaQueryDto
     * @return
     */
    @PostMapping("/media/list")
    public ResponseResult findMedia(PageRequest pageRequest,@RequestBody MediaQueryDto mediaQueryDto){
        return mediaService.findMedia(pageRequest,mediaQueryDto);
    }


    /**
     * 根据媒资id获取播放连接
     * @param mediaId
     * @return
     */
    @GetMapping("/media/preview/{mediaId}")
    public ResponseResult previewMedia(@PathVariable Long mediaId) throws Exception {
        return mediaService.previewMedia(mediaId);
    }

    /**
     * 根据id删除媒资信息
     * @param mediaId
     * @return
     */
    @DeleteMapping("/media/{mediaId}")
    public ResponseResult deleteMedia(@PathVariable Long mediaId) throws Exception {
        return mediaService.deleteMedia(mediaId);
    }

    /**
     * 媒资审核
     * @param mediaAuditDto
     * @return
     */
    @PutMapping("/media/audit")
    public ResponseResult auditMedia(@RequestBody MediaAuditDto mediaAuditDto){
        return mediaService.auditMedia(mediaAuditDto);
    }

    /**
     * 根据id查询媒资信息
     * @param mediaId
     * @return
     * @throws Exception
     */
    @GetMapping("/media/{mediaId}")
    public ResponseResult findMeidaById(@PathVariable Long mediaId) throws Exception {
        return mediaService.findMediaById(mediaId);
    }

    /**
     * 根据id查媒资播放地址
     * @param mediaId
     * @return vo
     */
    @GetMapping("/media/media-url/{mediaId}")
    public ResponseResult findMediaUrl(@PathVariable("mediaId")Long mediaId) throws Exception {
        return mediaService.findMediaUrl(mediaId);
    }
}
